<?php
/**
 * 
 * @author uguke
 *
 */
class Pagination {
	
	// 每页显示条数
	public $pageSize;
	// 数据总条数
	public $totalNums;
	// 当前页数
	public $currentPage;
	// 每次显示的总页数
	public $showPages;
	// 链接地址
	public $linkUrl;
		
	public function showStatic(){
		// 分页字符串
		$pagestr = "<div class=\"greenpage\">";
		// 页面总数
		$totalPages = ceil($this->totalNums/$this->pageSize);
		$page = floor($this->showPages/2);
		
		$begin = $this->currentPage-$page+1; //curpage = 5; $begin = 1;
		$end = $this->currentPage+$page;   //$end = 5;

		$begin = $begin<1?1:$begin; //$begin = 1
		$temp = $end-$begin;     //$temp = 4;
		
		if ($temp<$this->showPages-1){  //$shownum-1=7
			$temp = $this->showPages-$temp-1; //$temp = 3
			$end = $end+$temp; //$end=7
		}
		
		if ($end>$totalPages){
			$temp = $end-$totalPages;
			$begin = $begin-$temp;
			$end = $totalPages;
			$begin = $begin<1 ? 1:$begin;
		}
		
		// 当前页/总页数
		$pagestr .= "<span class=\"curandtotal\">共".$this->totalNums."条&nbsp;&nbsp;当前第$this->currentPage"."/".$totalPages."页</span>";
		
		// 是否是第一页
		if ($this->currentPage>1){
			$nextPage = $this->currentPage-1;
			$pagestr .= '<a href="'.$this->linkUrl.'1.html">[<]</a>';
			$pagestr .= '<a href="'.$this->linkUrl.$nextPage.'.html">[<<]</a>';
		} else {
			//$pagestr .= "<span class=\"disabled\">首页</span>";
			//$pagestr .= "<span class=\"disable\">下一页</span>";
		}

		// 经典分页循环
		$begin = $begin<1 ? 1:$begin;
		for ($c=$begin; $c<=$end; $c++){
			if ($c==$this->currentPage){
				$pagestr .= "<span class=\"current\">[$c]</span>";
			} else {
				$pagestr .= '<a href="'.$this->linkUrl.$c.'.html">['.$c.']</a>';
			}
		}
		
		// 是否是最后一页
		if ($this->currentPage<$totalPages){
			$prevPage = $this->currentPage+1;
			$pagestr .= '<a href="'.$this->linkUrl.$prevPage.'.html">[>>]</a>';
			$pagestr .= '<a href="'.$this->linkUrl.$totalPages.'.html">[>]</a>';
		} else {
			//$pagestr .= "<span class=\"disabled\">下一页</span>";
			//$pagestr .= "<span class=\"disabled\">末页</span>";
		}
		
		$pagestr .="</div>";
		
		return $this->getStyle().$pagestr;	
	}
	
	public function show(){
		// 分页字符串
		$pagestr = "<div class=\"greenpage\">";
		// 页面总数
		$totalPages = ceil($this->totalNums/$this->pageSize);
		$page = floor($this->showPages/2);
		
		$begin = $this->currentPage-$page+1; //curpage = 5; $begin = 1;
		$end = $this->currentPage+$page;   //$end = 5;

		$begin = $begin<1?1:$begin; //$begin = 1
		$temp = $end-$begin;     //$temp = 4;
		
		if ($temp<$this->showPages-1){  //$shownum-1=7
			$temp = $this->showPages-$temp-1; //$temp = 3
			$end = $end+$temp; //$end=7
		}
		
		if ($end>$totalPages){
			$temp = $end-$totalPages;
			$begin = $begin-$temp;
			$end = $totalPages;
			$begin = $begin<1 ? 1:$begin;
		}
		
		// 当前页/总页数
		$pagestr .= "<span class=\"curandtotal\">共".$this->totalNums."条&nbsp;&nbsp;当前第$this->currentPage"."/".$totalPages."页</span>";
		
		// 是否是第一页
		if ($this->currentPage>1){
			$nextPage = $this->currentPage-1;
			$pagestr .= "<a href=\"$this->linkUrl&page=1\">[<]</a>";
			$pagestr .= "<a href=\"$this->linkUrl&page=$nextPage\">[<<]</a>";
		} else {
			//$pagestr .= "<span class=\"disabled\">首页</span>";
			//$pagestr .= "<span class=\"disable\">下一页</span>";
		}

		// 经典分页循环
		$begin = $begin<1 ? 1:$begin;
		for ($c=$begin; $c<=$end; $c++){
			if ($c==$this->currentPage){
				$pagestr .= "<span class=\"current\">[$c]</span>";
			} else {
				$pagestr .= "<a href=\"$this->linkUrl&page=$c\">[$c]</a>";
			}
		}
		
		// 是否是最后一页
		if ($this->currentPage<$totalPages){
			$prevPage = $this->currentPage+1;
			$pagestr .= "<a href=\"$this->linkUrl&page=$prevPage\">[>>]</a>";
			$pagestr .= "<a href=\"$this->linkUrl&page=$totalPages\">[>]</a>";
		} else {
			//$pagestr .= "<span class=\"disabled\">下一页</span>";
			//$pagestr .= "<span class=\"disabled\">末页</span>";
		}
		
		$pagestr .="</div>";
		
		return $this->getStyle().$pagestr;
	}
	
	public function getStyle(){
		return "<style type=\"text/css\">
/*CSS greenpage style pagination*/
.greenpage {
   margin: 3px; text-align:left; padding:3px; font-size:12px;
}
.greenpage span.curandtotal {
   border:#b2e05d 1px solid; padding:2px 2px; font-weight:normal; color:#436A1C; margin-right: 2px; background:#CCDEAF;
}
.greenpage a {
	border: #ddd 1px solid; padding:2px 2px; color:#436A1C; margin-right: 2px; text-decoration:none; background:#CCDEAF;
}
.greenpage a:hover {
	border:#85bd1e 1px solid; color:#638425; background-color:#f1ffd6
}
.greenpage a:active {
 	border:#85bd1e 1px solid; color:#638425; background-color:#f1ffd6
}
.greenpage span.current {
 	border:#b2e05d 1px solid; padding:2px 2px; font-weight:bold; color:#C1D4A2; color:#fff; margin-right: 2px; background:#CCDEAF;
}
.greenpage span.disabled {
	border:#f3f3f3 1px solid; padding:2px 5px; color:#ccc; margin-right:2px;
}
</style>";
	}
}
?>